﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace SnmpForm.FaultDetect
{
    class DetectTask
    {
        MainForm _objFaultDetect = null;
        List<BusinessWrapper.Ap> listAsset = null;
        Dictionary<int, FaultDetectEvent> dictFaultDetect;
        public DetectTask(MainForm objFaultDetect)
        {
            _objFaultDetect = objFaultDetect;
        }

        public DetectTask(MainForm objFaultDetect, List<BusinessWrapper.Ap> aps)
        {
            _objFaultDetect = objFaultDetect;
            listAsset = aps;
            if (listAsset != null)
            {
                dictFaultDetect = new Dictionary<int, FaultDetectEvent>();
                foreach (BusinessWrapper.Ap ap in listAsset)
                {
                    FaultDetectEvent ev = new FaultDetectEvent();
                    ev.apid = ap.GetInfo().id;
                    ev.faultid = FaultCode.NORMAL;
                    ev.count = 0;
                    ev.repaired = true;
                    dictFaultDetect.Add(ap.GetInfo().id, ev);
                }
            }
        }
      
        public void ThreadFaultDetect()
        {
            while (true)
            {
                //检查在线状态
                CheckOnline();
                int nInterval = (int)Properties.Settings.Default.FaultDetectInterval;
                Thread.Sleep(1000*nInterval);
            }
        }

        private void CheckOnline()
        {
            foreach (BusinessWrapper.Ap ap in listAsset)
            {
                dictFaultDetect[ap.GetInfo().id].repaired = true;
                try
                {
                    ap.ScanNumOfstaOnAp();
                }
                catch (Lextm.SharpSnmpLib.Messaging.TimeoutException ex)
                {
                    //超时，销毁ap的snmp操作器
                    ap.RestructSnmpOperator();

                    Console.WriteLine(ex.Message.ToString());
                    if (dictFaultDetect[ap.GetInfo().id].faultid == FaultCode.OFFLINE)
                    {
                        dictFaultDetect[ap.GetInfo().id].count++;
                        dictFaultDetect[ap.GetInfo().id].repaired = false;
                    }
                    else
                    {
                        dictFaultDetect[ap.GetInfo().id].faultid = FaultCode.OFFLINE;
                        dictFaultDetect[ap.GetInfo().id].count = 1;
                        dictFaultDetect[ap.GetInfo().id].repaired = false;
                    }
                }
                catch (SocketException ex)
                {
                    Console.WriteLine(ex.Message.ToString());
                    if (dictFaultDetect[ap.GetInfo().id].faultid == FaultCode.OFFLINE)
                    {
                        dictFaultDetect[ap.GetInfo().id].count++;
                        dictFaultDetect[ap.GetInfo().id].repaired = false;
                    }
                    else
                    {
                        dictFaultDetect[ap.GetInfo().id].faultid = FaultCode.OFFLINE;
                        dictFaultDetect[ap.GetInfo().id].count = 1;
                        dictFaultDetect[ap.GetInfo().id].repaired = false;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message.ToString());
                    if (ex.Message.Contains("authentication"))
                    {
                        if (dictFaultDetect[ap.GetInfo().id].faultid == FaultCode.AUTH)
                        {
                            dictFaultDetect[ap.GetInfo().id].count++;
                            dictFaultDetect[ap.GetInfo().id].repaired = false;
                        }
                        else
                        {
                            dictFaultDetect[ap.GetInfo().id].faultid = FaultCode.AUTH;
                            dictFaultDetect[ap.GetInfo().id].count = 1;
                            dictFaultDetect[ap.GetInfo().id].repaired = false;
                        }
                    }
                    else
                    {
                        if (dictFaultDetect[ap.GetInfo().id].faultid == FaultCode.UNKNOW)
                        {
                            dictFaultDetect[ap.GetInfo().id].count++;
                            dictFaultDetect[ap.GetInfo().id].repaired = false;
                        }
                        else
                        {
                            dictFaultDetect[ap.GetInfo().id].faultid = FaultCode.UNKNOW;
                            dictFaultDetect[ap.GetInfo().id].count = 1;
                            dictFaultDetect[ap.GetInfo().id].repaired = false;
                        }
                    }
                }
                //AP标准故障
                if (dictFaultDetect[ap.GetInfo().id].count >= 3 || 
                    dictFaultDetect[ap.GetInfo().id].repaired == true)
                {
                    dictFaultDetect[ap.GetInfo().id].count = 0;
                    _objFaultDetect.BeginInvoke(_objFaultDetect.updateViewFaultAp, dictFaultDetect[ap.GetInfo().id]);
                }
            }
        }
    }
}
